/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Manager_Image_Utils;

import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Admin
 */
public class PopupMenu extends JPopupMenu {

    private File f;
    Constants c = new Constants();
    private JFileChooser chooser = new JFileChooser();

    public PopupMenu(File f) {
        this.f = f;
        setName("Options");
        JMenuItem copy = new JMenuItem("Copy", c.iconcopy);
        copy.setName("Copy");
        copy.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FileNameExtensionFilter filter = new FileNameExtensionFilter(".jpg", "jpg");
                chooser.setFileFilter(filter);
                FileNameExtensionFilter filter1 = new FileNameExtensionFilter(".gif", "gif");
                chooser.setFileFilter(filter1);
                FileNameExtensionFilter filter2 = new FileNameExtensionFilter(".png", "png");
                chooser.setFileFilter(filter2);
                FileNameExtensionFilter filter3 = new FileNameExtensionFilter(".tiff", "tiff");
                chooser.setFileFilter(filter3);
                int returnVal = chooser.showSaveDialog(null);
                if (returnVal == JFileChooser.APPROVE_OPTION) {

                    File file = new File(chooser.getSelectedFile().getPath() + getF().getAbsoluteFile().getName());

                    try {
                        Library.CopyFile(getF(), file);
                    } catch (IOException ex) {
                        Logger.getLogger(PopupMenu.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        this.add(copy);
        JMenuItem cut = new JMenuItem("Cut", c.iconcut);
        cut.setName("Cut");
        cut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FileNameExtensionFilter filter = new FileNameExtensionFilter(".jpg", "jpg");
                chooser.setFileFilter(filter);
                FileNameExtensionFilter filter1 = new FileNameExtensionFilter(".gif", "gif");
                chooser.setFileFilter(filter1);
                FileNameExtensionFilter filter2 = new FileNameExtensionFilter(".png", "png");
                chooser.setFileFilter(filter2);
                FileNameExtensionFilter filter3 = new FileNameExtensionFilter(".tiff", "tiff");
                chooser.setFileFilter(filter3);
                int returnVal = chooser.showSaveDialog(null);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = chooser.getSelectedFile();
                    System.out.print(file.getPath());
                    Library.CutFile(getF(), new File(file.getPath() + getF().getAbsoluteFile().getName()));
                }
            }
        });
        this.add(cut);
        JMenuItem delete = new JMenuItem("Delete", c.icdelete);
        delete.setName("Delete");
        delete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {


                boolean result = Library.DeleteFile(getF());
                if (result == true) {
                    JOptionPane.showConfirmDialog(null, "xóa file thành công", "Thông báo", JOptionPane.CLOSED_OPTION);
                } else {
                    JOptionPane.showConfirmDialog(null, "xóa file không thành công", "Thông báo", JOptionPane.CLOSED_OPTION);
                }

            }
        });
        this.add(delete);
        JMenuItem print = new JMenuItem("Print", c.iconprint);
        print.setName("Print");
        print.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MediaTracker tracker = new MediaTracker(new JPanel());

                try {
                    if (getF() == null) {
                        return;
                    }
                    Image img = ImageIO.read(getF());
                    tracker.addImage(img, 1);
                    tracker.waitForAll();
                    Library.print(img);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        this.add(print);
        
    }

    public File getF() {
        return f;
    }

    public void setF(File f) {
        this.f = f;
    }
}
